import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

import Login from "@/components/login.vue";
import Welcome from "@/components/welcome.vue";
//引入用户组件
import User from "@/components/user/user.vue";
import Rights from "../components/rights/roles.vue";



const routes = [{
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/welcome",
    component: Welcome,
    children: [{
        path: "/users",
        component: User,
      },
      {
        path: "/roles",
        component: Rights,
      },
    ]

  },
];

const router = new VueRouter({
  routes,
  mode: "hash",
});
router.beforeEach((to, from, next) => {
  // ...
  if (to.path !== '/login') {
    var token = window.sessionStorage.getItem('token')
    if (token) {
      next()
    } else {
      next({
        path: '/login'
      })
    }
  }
  next()
})


export default router;
